
import { EditBox, Label, NodeEventType, RenderData, dynamicAtlasManager, instantiate } from 'cc';
import { Enum } from 'cc';
import { _decorator, Component, Node, Material, Sprite, gfx, IAssembler, Color, __private, UI, SpriteFrame } from 'cc';
import { EDITOR } from 'cc/env';
import { UIBatchManager } from './UIBatchProto';
import { TimerManager } from './TimerManager';
import { Timer } from './Timer';
const { ccclass, property, executeInEditMode } = _decorator;


@ccclass('Test2D')
@executeInEditMode
export class Test2D extends Component {
    protected onLoad(): void {
        this.initEvent()
    }

    _timeID: Timer = null

    protected update(dt: number): void {
        TimerManager.GetInstance().Update(dt * 1000)
    }

    initEvent() {
        const btn = this.node.getChildByName("Button")
        const btn2 = this.node.getChildByName("Button2")
        const btn3 = this.node.getChildByName("Button3")
        const btn4 = this.node.getChildByName("Button4")
        const editBox = this.node.getChildByName("EditBox")
        const editBox2 = this.node.getChildByName("EditBox2")
        const content = this.node.getChildByName("ScrollView").getChildByName("view").getChildByName("content")
        const sps: Map<number, Node> = new Map<number, Node>()
        let visible = false
        for (let i = 1; i <= 12; i++) {
            const spr = this.node.getChildByName(`Sprite${i}`)
            sps.set(i, spr)

            spr.on(NodeEventType.TOUCH_END, () => {
                const sp = instantiate(spr)
                sp.active = true
                sp.name = "_child_" + String(content.children.length) + "_sprite_" + String(i)
                content.addChild(sp)
            })
        }
        btn.on('click', () => {
            this._timeID?.Finish()
            this._timeID = null
            content.destroyAllChildren()
        })
        btn2.on('click', () => {
            UIBatchManager.GetInstance().batchEnable = !UIBatchManager.GetInstance().batchEnable
            this.updateBtn2Label()
        })
        let add = 0
        btn3.on('click', () => {
            add += Math.ceil(Number(editBox2.getComponent(EditBox).string) ?? 1)
            if (!this._timeID) {
                this._timeID = TimerManager.GetInstance().CreateTimer(10, true, () => {
                    if (add <= 0) {
                        this._timeID?.Finish()
                        this._timeID = null
                    }
                    add -= 1
                    let num = Math.ceil(Number(editBox.getComponent(EditBox).string) ?? 0)
                    const rand = (Math.ceil(Math.random() * num)) >= sps.size ? sps.size : Math.ceil(Math.random() * num)
                    num = rand
                    num = (num > sps.size ? sps.size : num) + 1
                    if (!sps.get(num)) {
                        return
                    }
                    //随机数  1- 9 范围                   

                    const sp = instantiate(sps.get(num))
                    sp.active = true
                    sp.name = "_child_" + String(content.children.length) + "_sprite_" + String(num)
                    content.addChild(sp)

                })
            }
        })

        btn4.on('click', () => {
            visible = !visible
            sps.forEach((node, index) => {
                node.active = visible
            });
            btn4.getChildByName("Label").getComponent(Label).string = visible ? "展示" : "隐藏"
        })

        this.updateBtn2Label()
    }

    updateBtn2Label() {
        const btn2 = this.node.getChildByName("Button2")
        const label = btn2.getChildByName("Label")
        label.getComponent(Label).string = UIBatchManager.GetInstance().batchEnable ? "已开启合批" : "未开启合批"
    }

}
